#!/bin/python
# coding=utf8

from flask import Flask
from flask import render_template
from tools.DB import DB
import sys
reload(sys)
sys.setdefaultencoding('utf8')

app = Flask(__name__)

@app.errorhandler(404)
def page_not_found(e):
    return "Hi, I cannot find it!"


@app.route('/ace')
def ace():

    return render_template("student/list.htm")


@app.route('/tp1')
def tp1():
    user = {'nickname': 'Miguel'}  # fake user
    return render_template("tp1.txt", title='Home', user=user)


@app.route('/tp2')
def tp2():
    user = {'nickname': 'lida'}  # fake user
    items = [1, 2, 3, 4];
    sub2 = [
        {'xm': 'Tom', 'age': 13},
        {'xm': 'John', 'age': 24},
    ]

    return render_template("main.txt", title='Home', user=user, items=items, sub2=sub2)


@app.route('/tp3')
def tp3():
    db = DB("172.17.101.55", 'cms_rw', 'rainbow3101', 3306, 'rb_stat_test')
    count, res = db.query('select * from stat_user_new_daily')
    print "total rows", count

    for r in res:
        print r

    return render_template("db.txt", items=res)

if __name__ == '__main__':
    app.run(debug=True)
